﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Linq.Expressions;
using System.Data.Entity;

namespace NewsReport.Business
{
    public class BaseService<T> where T : class, new()
    {
        protected DbContext db = ContextFactory.GetCurrentContext();
        /// <summary>
        /// 添加实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Add(T entity)
        {
            db.Entry<T>(entity).State = EntityState.Added;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 删除实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Delete(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Deleted;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 更新实体
        /// </summary>
        /// <param name="entity"></param>
        /// <returns></returns>
        public bool Update(T entity)
        {
            db.Set<T>().Attach(entity);
            db.Entry<T>(entity).State = EntityState.Modified;
            return db.SaveChanges() > 0;
        }
        /// <summary>
        /// 查询实体
        /// </summary>
        /// <param name="whereLamba"></param>
        /// <returns></returns>
        public T Find(Expression<Func<T, bool>> whereLamba)
        {
            return db.Set<T>().FirstOrDefault(whereLamba);
        }
        /// <summary>
        /// 查询获取列表
        /// </summary>
        /// <param name="whereLamba"></param>
        /// <returns></returns>
        public IQueryable<T> GetList(Expression<Func<T, bool>> whereLamba)
        {
            return db.Set<T>().Where(whereLamba);
        }
        /// <summary>
        /// 查询列表带排序
        /// </summary>
        /// <typeparam name="Tkey"></typeparam>
        /// <param name="whereLamba"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetList<Tkey>(Expression<Func<T, bool>> whereLamba, Expression<Func<T, Tkey>> orderBy, bool isAsc = true)
        {
            if (isAsc)
            {
                return db.Set<T>().Where(whereLamba).OrderBy(orderBy);
            }
            else
            {
                return db.Set<T>().Where(whereLamba).OrderByDescending(orderBy);
            }
        }
        /// <summary>
        /// 查询列表分页排序
        /// </summary>
        /// <typeparam name="Tkey"></typeparam>
        /// <param name="pageIndex"></param>
        /// <param name="pageSize"></param>
        /// <param name="rowCount"></param>
        /// <param name="whereLamba"></param>
        /// <param name="orderBy"></param>
        /// <param name="isAsc"></param>
        /// <returns></returns>
        public IQueryable<T> GetList<Tkey>(int pageIndex, int pageSize, ref int rowCount, Expression<Func<T, bool>> whereLamba, Expression<Func<T, Tkey>> orderBy, bool isAsc = true)
        {
            pageIndex = pageIndex - 1 < 0 ? 1 : pageIndex;
            pageSize = pageSize - 1 < 0 ? 10 : pageSize;
            rowCount = db.Set<T>().Where(whereLamba).Count();
            if (isAsc)
            {
                return db.Set<T>().Where(whereLamba).OrderBy(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }
            else
            {
                return db.Set<T>().Where(whereLamba).OrderByDescending(orderBy).Skip((pageIndex - 1) * pageSize).Take(pageSize);
            }
        }
    }
}
